<template>
  <div>
    <el-row>
      <el-col :span="12">
        <el-button size="mini" @click="add" type="success">新增</el-button>
        <el-button size="mini" @click="update" type="info">修改</el-button>
        <el-button size="mini" @click="delet" type="danger">删除</el-button>
      </el-col>
      <el-col :span="12"></el-col>
    </el-row>
    <template>
      <el-table
        :data="clist"
        border
        style="width: 100%;margin-top:10px"
        max-height="500px"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column type="index" label="序号" width="300px"></el-table-column>
        <el-table-column prop="classify" label="类别"></el-table-column>
        <el-table-column prop="type" label="类型"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit( scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="30%">
      <span>
        <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-ruleForm">
          <el-form-item label="商品类别" prop="classify">
            <el-input v-model="form.classify"></el-input>
          </el-form-item>
          <el-form-item label="商品类型" prop="type">
            <el-input v-model="form.type"></el-input>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="save">取 消</el-button>
        <el-button type="primary" @click="doSave('form')">{{title}}</el-button>
      </span>
    </el-dialog>
    <!-- <el-pagination
      class="top"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[1, 3, 5, 7]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total.total"
    ></el-pagination>-->
  </div>
</template>

<script>
import { mapActions, mapState, mapMutations } from "vuex";
export default {
  data() {
    return {
      dialogVisible: false,
      title: "新增",
      form: {},
      arr: [],
      currentPage: 1,
      rules: {
        classify: [
          { required: true, message: "请输入商品类别", trigger: "blur" },
          { min: 1, max: 5, message: "长度在 1 到 5 个字符", trigger: "blur" },
        ],
        type: [
          { required: true, message: "请输入商品类型", trigger: "blur" },
          { min: 1, max: 5, message: "长度在 1 到 5 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    ...mapActions("classify", [
      "getListss",
      "addLists",
      "updateLists",
      "deleteLists",
      "getListFirst",
    ]),
    ...mapMutations("classify", ["SET_CURRENT", "SET_PAGESIZE"]),
    //新增
    add() {
      this.dialogVisible = true;
      this.form = {};
      this.title = "新增";
    },
    //取消
    save() {
      this.dialogVisible = false;
      this.form = {};
    },
    //确定
    doSave(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          if (this.form.id) {
            // console.log(修改);
            await this.updateLists(this.form);
            this.$message({
              message: "恭喜你，修改成功",
              type: "success",
            });
          } else {
            // console.log(新增);
            await this.addLists(this.form);
            this.$message({
              message: "恭喜你，新增成功",
              type: "success",
            });
          }
        } else {
          this.$message("请填写表单再提交");
          this.$refs[formName].resetFields();
          return false;
        }
      });
      this.dialogVisible = false;
    },
    //修改
    async handleEdit(row) {
      // console.log(row);
      this.dialogVisible = true;
      this.form = { ...row };
      this.title = "修改";
    },
    //外部删除
    async delet() {
      if (!this.arr.length) {
        this.$message({
          message: "请选择要删除的项",
          type: "warning",
        });
      } else {
        let lit = [];
        this.arr.forEach((v) => {
          lit.push(v.id);
        });
        await this.handleDelete(lit.join(","));
      }
    },
    //外部修改
    async update() {
      if (this.arr.length != 1) {
        this.$message("请选择一项修改");
      } else {
        await this.handleEdit(this.arr[0]);
      }
    },
    //删除
    handleDelete(index) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          // console.log(index);
          await this.deleteLists(index);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      this.getListFirst();
      // this.getListss();
      // console.log(index, row);
    },
    //复选框
    handleSelectionChange(val) {
      this.arr = val;
    },
    //每页
    async handleSizeChange(val) {
      // console.log(val);
      await this.SET_PAGESIZE(val);
      await this.getListss();
    },
    //当前页
    async handleCurrentChange(val) {
      await this.SET_CURRENT(val);
      await this.getListss();
    },
  },
  computed: {
    ...mapState("classify", ["clist", "total", "pageSize"]),
  },
  mounted() {
    this.getListFirst();
    this.getListss();
    // console.log( this.getListss());
    // console.log(this.list());
  },
};
</script>

<style scoped>
.top {
  margin-top: 20px;
}
</style>
